﻿@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>校区信息管理</title>
    <link href="~/Scripts/layui/css/layui.css" rel="stylesheet">
</head>

<!-- 添加/编辑校区的弹框 -->
<div id="campusModal" style="display: none; padding: 20px;">
    <form class="layui-form" id="campusForm" lay-filter="campusForm">
        <!-- 隐藏域，用于存放ID，添加时为空，编辑时赋值 -->
        <input type="hidden" name="CampusID" id="CampusID">

        <div class="layui-form-item">
            <label class="layui-form-label">校区名称</label>
            <div class="layui-input-block">
                <input type="text" name="CampusName" id="CampusName" required lay-verify="required|checkCampusName" placeholder="请输入校区名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">校区备注</label>
            <div class="layui-input-block">
                <textarea name="CampusReMark" id="CampusReMark" placeholder="请输入校区备注" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submitCampus">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<body>
    <!-- 容器，用于布局表单和表格 -->
    <div class="layui-fluid" style="padding: 15px;">

        <!-- 校区管理搜索表单 -->
        <div class="layui-form layui-row layui-col-space16" lay-filter="campus-search-form">
            <div class="layui-col-md4">
                <div class="layui-form-item">
                    <label class="layui-form-label">校区名称：</label>
                    <div class="layui-input-block">
                        <input type="text" id="txtCampusName" name="campusName" placeholder="请输入校区名称" class="layui-input" lay-affix="clear">
                    </div>
                </div>
            </div>

            <div class="layui-col-md4">
                <div class="layui-form-item">
                    <label class="layui-form-label">&nbsp;</label>
                    <div class="layui-input-block">
                        <button class="layui-btn" id="btnCampusSearch" lay-filter="campus-search-btn">搜索</button>
                        <button type="button" class="layui-btn" id="btnAddCampus">添加</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 表格 -->
        <table class="layui-hide" id="tblCampusData" lay-filter="tblCampusData"></table>
    </div>

    <script src="~/Scripts/layui/layui.js"></script>
    <script>
        layui.use(['table', 'jquery', 'layer', 'form'], function () {
            var table = layui.table;
            var $ = layui.$;
            var layer = layui.layer;
            var form = layui.form;

            // 表格渲染
            table.render({
                elem: "#tblCampusData",
                url: "/School/GetCampusData",
                cols: [[
                    { type: 'radio', title: '😊', fixed: 'left' },
                    { field: 'CampusID', title: '校区ID', width: 200, sort: true },
                    { field: 'CampusName', title: '校区名称', width: 300 },
                    { field: 'CampusReMark', title: '校区备注' },
                    {
                        fixed: 'right', title: '操作', width: 200,
                        templet: '#toolbar'
                    }
                ]],
                page: true,
                limit: 5,
                limits: [5, 10, 15, 20, 50, 100]
            });

            //【添加按钮】点击事件
            $("#btnAddCampus").click(function () {
                // 清空表单
                $('#campusForm')[0].reset();
                // 清空隐藏域ID
                $('#CampusID').val('');
                // 弹出添加框
                layer.open({
                    type: 1,
                    title: '添加校区',
                    area: ['500px', '300px'],
                    content: $('#campusModal'),
                    success: function (layero, index) {
                        form.render();
                    }
                });
            });

            //【编辑/删除按钮】点击事件（监听表格工具条）
            table.on('tool(tblCampusData)', function (obj) {
                var data = obj.data; // 获取当前行数据
                // 编辑逻辑
                if (obj.event === 'edit') {
                    form.val('campusForm', {
                        "CampusID": data.CampusID,
                        "CampusName": data.CampusName,
                        "CampusReMark": data.CampusReMark
                    });
                    layer.open({
                        type: 1,
                        title: '编辑校区',
                        area: ['500px', '300px'],
                        content: $('#campusModal'),
                        success: function (layero, index) {
                            form.render();
                        }
                    });
                }
                // 删除逻辑
                else if (obj.event === 'del') {
                    layer.confirm('确定要删除该校区吗？删除后不可恢复！', {
                        icon: 3, title: '警告'
                    }, function (index) {
                        $.ajax({
                            url: "/School/DeleteCampus",
                            type: "POST",
                            data: { campusId: data.CampusID },
                            success: function (res) {
                                if (res.code === 0) {
                                    layer.msg(res.msg, { icon: 1 });
                                    obj.del(); // 前端表格删除当前行
                                } else {
                                    layer.msg(res.msg, { icon: 5 });
                                }
                            },
                            error: function () {
                                layer.msg("网络错误，删除失败！", { icon: 5 });
                            }
                        });
                        layer.close(index);
                    });
                }
            });

            //【表单提交】事件
            form.on('submit(submitCampus)', function (data) {
                var formData = data.field;
                var url = formData.CampusID ? "/School/UpdateCampus" : "/School/AddCampus";

                $.ajax({
                    url: url,
                    type: "POST",
                    data: formData,
                    success: function (res) {
                        if (res.code === 0) {
                            layer.msg(res.msg, { icon: 1 });
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            table.reload('tblCampusData');
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    },
                    error: function () {
                        layer.msg("操作失败，请联系管理员！", { icon: 5 });
                    }
                });
                return false;
            });

            //【自定义验证规则】验证校区名称是否已存在
            form.verify({
                checkCampusName: function (value) {
                    var isExists = false;
                    var currentId = $('#CampusID').val();

                    $.ajax({
                        url: "/School/IsCampusNameExists",
                        type: "GET",
                        async: false,
                        data: {
                            campusName: value,
                            excludeId: currentId
                        },
                        success: function (res) {
                            if (res.code === 1) {
                                isExists = true;
                            }
                        }
                    });

                    if (isExists) {
                        return '当前校区已存在，请重新填写！';
                    }
                }
            });

            // 搜索功能
            $("#btnCampusSearch").click(function () {
                table.reload('tblCampusData', {
                    page: {
                        curr: 1
                    },
                    where: {
                        strCampusName: $("#txtCampusName").val()
                    }
                });
            });
        });
    </script>

    <!-- 操作列模板 -->
    <script type="text/html" id="toolbar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

</body>
</html>